package com.xlhj.yucloud.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xlhj.yucloud.entity.SysUserRole;
import com.xlhj.yucloud.mapper.SysUserRoleMapper;
import com.xlhj.yucloud.service.SysUserRoleService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: liucaijing
 * @description: SysUserRoleServiceImpl
 * @date: 2021/10/10 9:56
 */
@Service
public class SysUserRoleServiceImpl extends ServiceImpl<SysUserRoleMapper, SysUserRole> implements SysUserRoleService {

    @Override
    public boolean saveBatchUserRole(Long userId, Long[] roleIds) {
        List<SysUserRole> userRoleList = new ArrayList<>();
        for (Long roleId : roleIds) {
            SysUserRole userRole = new SysUserRole();
            userRole.setUserId(userId);
            userRole.setRoleId(roleId);
            userRoleList.add(userRole);
        }
        return this.saveBatch(userRoleList);
    }

    @Override
    public void removeByUserId(Long userId) {
        LambdaQueryWrapper<SysUserRole> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysUserRole::getUserId, userId);
        this.remove(queryWrapper);
    }

    @Override
    public void removeByUserIds(Long[] userIds) {
        for (Long userId : userIds) {
            this.removeByUserId(userId);
        }
    }
}
